/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package testing_class;

import java.net.SocketException;
import java.net.UnknownHostException;
import services.ConvIntByte;
import stendcontrol.AbstractMessage;
import stendcontrol.Configurator;
import stendcontrol.MeasureSeqMessage;
import stendcontrol.MessageBuilder;
import stendcontrol.MessageListener;
import stendcontrol.StatusMessage;
import stendcontrol.UdpServer;

/**
 *
 * @author minaev
 */
public class MessageBuilder_Test {

    public static int cnt1000=0;
    public static void main(String[] args) throws UnknownHostException, SocketException, InterruptedException {
        Configurator cnf = Configurator.getConf();

        UdpServer udps = new UdpServer("127.0.0.255",
                (Integer) cnf.getProperty(Configurator.UDP_IN_PORT),
                (Integer) cnf.getProperty(Configurator.UDP_OUT_PORT));

        MessageBuilder mb = new MessageBuilder();

        Thread t_udps = new Thread(udps);
        Thread t_mb = new Thread(mb);
        
        MessageListener ml = new MessageListener() {

            @Override
            public void msgBuilded(AbstractMessage msg) {
                if (msg.getTypeMsg() == 1) {
                    MeasureSeqMessage mmsg = (MeasureSeqMessage) msg;
                    System.out.println("Measure size:" + mmsg.getSize());
                    if (mmsg.getSize() == 1000) {
                        cnt1000++;
                    }
                } else if (msg.getTypeMsg() == 2) {
                    StatusMessage smsg = (StatusMessage) msg;
                    System.out.println("Status message status:"
                            + smsg.getStatus());
                }
            }
        };

        udps.addTransmitListener(mb);
        mb.addMessageListener(ml);
        udps.startTransmitToExp();

        t_mb.start();
        t_udps.start();

        UDP_Fluder udpf = new UDP_Fluder("127.0.0.255", 1);
        udpf.start();

        Thread.sleep(10000);

        udpf.shutdown();
        udps.stopUDPThread();
        mb.stopBuilding();
        System.out.println("END!!!");
        System.out.println("cnt1000:"+cnt1000);
    }
}
